define(
	[
		'require'
	],
	function(require){
		var Controller = Ember.Controller.extend({
			needs: ["businessDetail", "businessCenter"],

			business: Ember.computed.alias('controllers.businessDetail.model'),

			pinTimePoint: Ember.computed.alias('controllers.businessDetail.pinTimePoint'),

			businessIdChange: Ember.observer("business.id", function(){
				this.set('keyWord', '');
			}),

			initTime: function(){
		        var ctrl = this;
		        var startDate = ctrl.get("startDate");
		        var endDate = ctrl.get("endDate");
		        if(this.get("clickTimePoint") != null ){
		        	var date = new Date(this.get("clickTimePoint"));
		        	ctrl.set("endDate",new Date(+date+60000));
		        	if((isNaN(startDate))){
		        		ctrl.set("startDate",new Date(+date+60000)-60000);
		        	}else{
		        		ctrl.set("startDate",new Date(+date+60000)-(Math.abs(+endDate-(+startDate))));
		        	}
		        	
		        	
		        	setTimeout(function(){
		        		var startDate = APM.util.formatDate(new Date(+ctrl.get("startDate")), "yyyy/MM/dd H:mm");
		        		var endDate = APM.util.formatDate(new Date(+ctrl.get("endDate")), "yyyy/MM/dd H:mm");
		        		$('#config-demo').val(startDate+' - '+endDate);
		        	});
		        	
		        }else{
		        	var sdate = new Date(new Date() - timeD_value - 60000-60000);
			        sdate.setSeconds(0);
			        sdate.setMilliseconds(0); 
			        ctrl.set("startDate",new Date(+sdate));
			        var edate = new Date(new Date() - timeD_value-60000);
			        edate.setSeconds(0);
			        edate.setMilliseconds(0); 
			        ctrl.set("endDate",new Date(+edate));
		        }
		    },

			clickTimePoint: Ember.computed.oneWay('controllers.businessDetail.clickTimePoint'),

			// timePointLisenterEvent:Ember.observer("clickTimePoint", function(key, value){
			// 	var view = Broada.getView("businessTimeRangesTopo");
				// if(this.get("clickTimePoint") != null){
				// 	var date = new Date(this.get("clickTimePoint"));
				// 	if(typeof view != "undefined"){
				// 		var range = view.getTimeRange().endDate - view.getTimeRange().startDate;	
				// 		if(range == 60000){
				// 			this.set("startDate", new Date(date.getTime()-range+60000));
				// 			this.set("endDate", new Date(date.getTime()+60000));
				// 		}else{
				// 			this.set("startDate",new Date(date.getTime()-range+60000));
				// 			this.set("endDate", new Date(date.getTime()+60000));
				// 		}
				// 	}
					
			// 		typeof view != "undefined"?view.set("btnTxt", date?APM.util.formatDate(date, "f"):null):null;
			// 		this.requireAlerts();
			// 	}
				
			// }),

			alarmColumns: [
				{
					headerText: "级别",
					dataKey:"alertSevertiyName",
					width: 60,
					allowSort: false,
					ensurePxWidth: true,
					cellFormatter: function (args, colume) {
						if (args.row.type & $.wijmo.wijgrid.rowType.data) {
							var ctrl = this.get("controller");
							var date = args.row.data.alertSevertiyName;
							if("严重"==date){
								var cb = $("<div class='label label-important'></div>").text(date);
								cb.appendTo(args.$container);
							}
							if("警告"==date){
								var cb = $("<div class='label label-warning'></div>").text(date);
								cb.appendTo(args.$container);
							}
							if("提醒"==date){
								var cb = $("<div class='label label-info'></div>").text(date);
								cb.appendTo(args.$container);
							}
							return true;
						}
					}
				},
				{
					headerText: "告警来源",
					dataKey:"source",
					width: 350,
					textAlignment:"left",
					allowSort: false,
					ensurePxWidth: true
				},
				{
					headerText: "告警类型",
					dataKey:"alertTypeName",
					width: 100,
					allowSort: false,
					ensurePxWidth: true
				},
				{
					headerText: "开始时间",
					dataKey:"firstTime",
					width: 120,
					allowSort: false,
					ensurePxWidth: true,
					cellFormatter: function (args, colume) {
						if (args.row.type & $.wijmo.wijgrid.rowType.data) {
							var ctrl = this.get("controller");
							var date = new Date(args.row.data.firstTime);
							var  dateFor=APM.util.formatDate(date, "yyyy-MM-dd H:mm");
							var cb = $("<div></div>").text(dateFor);
							cb.appendTo(args.$container);
							return true;
						}
					}
				},
				{
					headerText: "最后一次告警时间",
					dataKey:"lastTime",
					width: 150,
					allowSort: false,
					ensurePxWidth: true,
					cellFormatter: function (args, colume) {
						if (args.row.type & $.wijmo.wijgrid.rowType.data) {
							var ctrl = this.get("controller");
							var date = new Date(args.row.data.lastTime);
							var  dateFor=APM.util.formatDate(date, "yyyy-MM-dd H:mm");
							var cb = $("<div></div>").text(dateFor);
							cb.appendTo(args.$container);
							return true;
						}
					}
				},
				{
					headerText: "描述",
					dataKey:"desc",
					allowSort: false,
					cellFormatter: function (args, colume) {
						if (args.row.type & $.wijmo.wijgrid.rowType.data) {
							var data = args.row.data;
							var value = data.desc;

							var $item = $('<div class="column-summary"></div>').appendTo(args.$container);
							$item.html(APM.util.escape(value));

							args.$container.attr("title",value);
							return true;
						}
					}
				}
			],

			alerts: Ember.A(),

			keyUpEnent: function(){
				var ctrl = this;
				setTimeout(function(){
					$("#mouseKeyUp").on("keyup",function(event){
						if(event.keyCode == 13){
							ctrl.requireAlerts();
						};
					});
				},0)
				
			},

			requireAlerts: function() {
				var ctrl = this,
					nodeId = this.get("model.id");

				var startDate = this.get("startDate"),
					endDate = this.get("endDate");

				var params = {
					"appId":this.get("business.id"),
					id: nodeId,
					type:'service',
					rows: 10,
					keyWord: this.get("keyWord"),
					startDate: +startDate,
					endDate: +endDate,
					panelId: overviewOrCenterSwitch
				};
				//开发调试模式参数
				if( isLocalDebugModel ){
					params = undefined;
				}

				return this.store.ajax("GET", "alarms/latest", params)
				.then(function(response){
					var alerts = response.alerts;
					
					if (alerts.length == 0) {
						ctrl.set("pageIndex",0);
						ctrl.set("pageSize",10);
						ctrl.set("totalRows",0);
					}else{
						ctrl.set("pageIndex",null);
						ctrl.set("pageSize",null);
						ctrl.set("totalRows",null);
					}
					
					ctrl.set("alerts", alerts);
					return response;
				});
			},

			actions: {
				search: function() {
					this.set("pageIndex", 0);
					this.requireAlerts();
					this.get("clickTimePoint");
				},

				timePointChange: function(timeRange){
					this.set("startDate",timeRange.startDate);
					this.set("endDate",timeRange.endDate);
					this.requireAlerts();
				},
				timeRangesChange: function(timeRange){
					var ctrl = this;
					var pinTimePoint = ctrl.get("controllers.businessDetail").pinTimePoint;

						ctrl.set("startDate", timeRange.startDate);
						ctrl.set("endDate", timeRange.endDate);
			
					
					var view = Broada.getView("businessTimeRangesTopo");
					if(ctrl.get("pinTimePoint") != timeRange.startDate){
						ctrl.set("pinTimePoint", null);
					}

/*					if(timeRange.selectVal == "n1M"||timeRange.selectVal == "n1d"){
						ctrl.set("startDate", timeRange.startDate);
						ctrl.set("endDate", timeRange.endDate);
					};
*/
					ctrl.set("startDate", timeRange.startDate);
					ctrl.set("endDate", timeRange.endDate);

					ctrl.set("pageIndex", 0);
					ctrl.requireAlerts();
				}
			}
		});

		APM.BusinessDetailNodedetailAlarmsController = Controller;

		return Controller;
	}
);	